package cn.myfrank;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;

import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;

import net.minecraft.entity.player.HungerManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;

import static net.minecraft.server.command.CommandManager.*;

public class HungryPeacfulMode implements ModInitializer {
	// This logger is used to write text to the console and the log file.
	// It is considered best practice to use your mod id as the logger's name.
	// That way, it's clear which mod wrote info, warnings, and errors.
    public static final Logger LOGGER = LoggerFactory.getLogger("ymf-hpm");
    private static final int MAX_FOODLEVEL = 20;
    private static final float MAX_EXHAUSTION = 4.0f;
	@Override
	public void onInitialize() {
		// This code runs as soon as Minecraft is in a mod-load-ready state.
		// However, some things (like resources) may still be uninitialized.
		// Proceed with mild caution.

		// Command of query foodLevel, saturationLevel & exhaustion.
		CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(literal("hpmCheck")
		.requires(source -> source.hasPermissionLevel(2))
        .executes(ctx -> {
			ServerCommandSource source = ctx.getSource(); 
			@Nullable ServerPlayerEntity player = source.getPlayer();
			if(player!=null) {
				HungerManager hm = player.getHungerManager();
				String tofeed = String.format("[HPM] foodLevel: %d, saturationLevel: %f, exhaustion: %f.",
					hm.getFoodLevel(),hm.getSaturationLevel(),hm.getExhaustion());
				ctx.getSource().sendFeedback(() -> Text.literal(tofeed), false);
			} else {
				ctx.getSource().sendFeedback(() -> Text.literal("[HPM] Not a player."), false);
			}
			return 1;
		})));
		
		CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(literal("hpmSetFoodLevel")
		.requires(source -> source.hasPermissionLevel(2))
		.then(argument("value", IntegerArgumentType.integer(0, MAX_FOODLEVEL))
        .executes(ctx -> {
        	final int value = IntegerArgumentType.getInteger(ctx, "value");
			ServerCommandSource source = ctx.getSource(); 
			@Nullable ServerPlayerEntity player = source.getPlayer();
			if(player!=null) {
				HungerManager hm = player.getHungerManager();
				hm.setFoodLevel(value);
				String tofeed = String.format("[HPM] foodLevel set to %d.", hm.getFoodLevel());
				ctx.getSource().sendFeedback(() -> Text.literal(tofeed), false);
			} else {
				ctx.getSource().sendFeedback(() -> Text.literal("[HPM] Not a player."), false);
			}
			return 1;
		}))));
		
		CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(literal("hpmSetSaturationLevel")
		.requires(source -> source.hasPermissionLevel(2))
		.then(argument("value", FloatArgumentType.floatArg(0.0f, (float)MAX_FOODLEVEL))
        .executes(ctx -> {
        	final float value = FloatArgumentType.getFloat(ctx, "value");
			ServerCommandSource source = ctx.getSource(); 
			@Nullable ServerPlayerEntity player = source.getPlayer();
			if(player!=null) {
				HungerManager hm = player.getHungerManager();
				hm.setSaturationLevel(value);
				String tofeed = String.format("[HPM] saturationLevel set to %f.", hm.getSaturationLevel());
				ctx.getSource().sendFeedback(() -> Text.literal(tofeed), false);
			} else {
				ctx.getSource().sendFeedback(() -> Text.literal("[HPM] Not a player."), false);
			}
			return 1;
		}))));
		
		CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(literal("hpmSetExhaustion")
		.requires(source -> source.hasPermissionLevel(2))
		.then(argument("value", FloatArgumentType.floatArg(0.0f, MAX_EXHAUSTION))
        .executes(ctx -> {
        	final float value = FloatArgumentType.getFloat(ctx, "value");
			ServerCommandSource source = ctx.getSource(); 
			@Nullable ServerPlayerEntity player = source.getPlayer();
			if(player!=null) {
				HungerManager hm = player.getHungerManager();
				hm.setExhaustion(value);
				String tofeed = String.format("[HPM] exhaustion set to %f.", hm.getExhaustion());
				ctx.getSource().sendFeedback(() -> Text.literal(tofeed), false);
			} else {
				ctx.getSource().sendFeedback(() -> Text.literal("[HPM] Not a player."), false);
			}
			return 1;
		}))));
		LOGGER.info("[ymf-hpm] Initialized!");
	}
}